package sort.select;

/**
 * 选择排序： 选择前n个数字中最小的，和第一个数字交换
 */
public class SelectSort {

    public void sort(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if(arr[j]< arr[minIndex]){
                    minIndex = j;
                }

            }

            //minIndexi
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }

    public void printArr(int[] arr){
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        for (int i = 0; i < arr.length; i++) {
            builder.append(arr[i]+",");
        }
        builder.append("]");
        System.out.println(builder.toString());
    }

    public static void main(String[] args) {
        int[] ints = {1, 4, 2, 0, 7, 8, 8, 11, 13};
        SelectSort selectSort = new SelectSort();
        selectSort.sort(ints);
        selectSort.printArr(ints);
    }

}
